angular.module('imsApp').controller('advertisersController', ['$scope', '$rootScope', '$state', 'AdvertisersManager','ImsAlert','$sce','ImsDialog', function ($scope, $rootScope, $state, AdvertisersManager,ImsAlert,$sce,ImsDialog) {
    $scope.$sce=$sce;
    $scope.search = {pageNo:$state.params.pageNo || 1,pageSize:$state.params.pageSize||10,keyword:$state.params.keyword,advertiserType:$state.params.advertiserType,status:$state.params.status};
    $scope.keyword=$scope.search.keyword;
    $scope.paginationConf = {
        currentPage: $state.params.pageNo || 1,
        itemsPerPage: $state.params.pageSize || 10,
        pagesLength: 9,
        search: 0,
        onChange: function () {
            $scope.search.pageNo = this.currentPage;
            $scope.search.pageSize = this.itemsPerPage;
            $state.go("advertisers", $scope.search)

        }
    };

    $scope.searchChange=function () {
        $scope.search.pageNo=1;
        $state.go("advertisers", $scope.search)
    }


    $scope.searchClick=function(keyword){
        $scope.search.keyword=keyword;
        $scope.search.pageNo=1;
        $state.go("advertisers", $scope.search)


    }
    //广告主删除
    $scope.deletes = function (key) {
        $scope.search.pageNo=1;
        $scope.search[key]=null;
        $state.go($state.current.name, $scope.search)

    }

    $scope.deleteAdvertisers=function () {
        var ids=[];
        $scope.advertisersList.pageList.forEach(function (advertiser) {
            if(advertiser.checked){
                ids.push(advertiser.advertiserId)
            }
        })
        if(!ids.length){
            return false;
        }else{
            ImsDialog.open({title: "删除确认", message: "您确定要删除数据吗?"}).then(function (a) {
                if (a == 'ok') {
                    AdvertisersManager.advertisersDelete(ids).then(function (result) {
                        $state.reload();
                    })
                }
            })
        }
    }



    AdvertisersManager.advertisersList([$scope.search.pageNo, $scope.search.pageSize], $scope.search).then(function (result) {
        $scope.advertisersList = result.data;
        $scope.paginationConf.totalItems = result.data.total;

    })
    var table = JSON.parse(localStorage.getItem('codeTable'));
    $scope.advertiserType = {51: table[51], 52: table[52]};
    $scope.status = {2: table[2], 1: table[1]};

    $scope.deletes = function (key) {
        $scope.search[key]=null;
        $scope.search.pageNo=1;
        $state.go("advertisers", $scope.search)
    }

    $scope.resetAdvPassword=function(advId,name){
        AdvertisersManager.advertisersResetPassword(advId,{userName:name}).then(function(result){
          if(result.data.c=='s'){
              ImsAlert.open({message:name+"的密码重置成功"})
          }
        })

    }


    $scope.changeDisabled=function(id,status){
        if(status==1){
            status=2;
        }else{
            status=1;
        }
        AdvertisersManager.advertisersStatusEdit(id,{status:status}).then(function(result){
            console.log(result);
            if(result.data.c=="s"){
                $state.reload();
            }
        })


        /*  CampaignsManager.campaignsStatusEdit(item.campaignId,{status:status}).then(function(result){
         $state.reload();
         })*/
    }
}])

